package com.auction.controller;

import com.auction.common.core.controller.MyBaseController;
import com.auction.common.utils.ReturnResult;
import com.auction.entity.UserAddr;
import com.auction.service.UserAddrService;
import com.auction.util.UpdateDefaultAddr;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api")
@Api(tags = "用户收货地址管理")
public class UserAddrController extends MyBaseController {
    @Autowired
    private UserAddrService userAddrService;

    @GetMapping("/getUserAddrbByUserId")
    @ApiOperation("根据用户id获取收货地址")
    public ReturnResult getUserAddrbByUserId(int user_id) {
        return success("用户收货地址",userAddrService.getUserAddrByUserId(user_id));
    }

    @PostMapping("/addUserAddr")
    @ApiOperation("添加用户收货地址")
    public ReturnResult addUserAddr(@RequestBody UserAddr userAddr) {
        int res=userAddrService.saveEntity(userAddr);
        return saveResult(res);
    }

    @DeleteMapping("/deleteUserAddr")
    @ApiOperation("删除用户收货地址")
    public ReturnResult deleteUserAddr(int addr_id) {
        return deleteResult(userAddrService.deleteEntity(addr_id));
    }

    @PutMapping("/updateUserAddr")
    @ApiOperation("修改用户收货地址")
    public ReturnResult updateUserAddr(@RequestBody UserAddr userAddr) {
        int res=userAddrService.updateEntity(userAddr);
        return saveResult(res);
    }

    @PostMapping("/updateDefault")
    @ApiOperation("修改默认地址")
    public ReturnResult updateDefault(@RequestBody UpdateDefaultAddr updateDefaultAddr) {
        int res=userAddrService.updateDefault(updateDefaultAddr.getNewAddr(), updateDefaultAddr.getOldAddr());
        return updateResult(res);
    }
}
